﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TSU.FAMC.ComputerEngineering.Classes.BooleanMaths.Functions;
using TSU.FAMC.ComputerEngineering.Classes.BooleanMaths.Functions.Readers;
using TSU.FAMC.ComputerEngineering.Classes.BooleanMaths.Fundamental;
using TSU.FAMC.ComputerEngineering.Classes.Diagnostics.PathDelayFaults;

namespace PDFTestSequence
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Button1Click(object sender, EventArgs e)
        {
            // Verifying tests
            if (openFileDialog1.FileNames.Length == 0 || openFileDialog2.FileNames.Length == 0)
            {
                MessageBox.Show("Select original and compacted tests!");
                return;
            }

            if (openFileDialog1.FileNames.Length != openFileDialog2.FileNames.Length)
            {
                MessageBox.Show("You should select identical sets of tests (original and compacted)!");
                return;
            }


            // selecting output folder
            if (folderBrowserDialog1.ShowDialog() != DialogResult.OK)
            {
                MessageBox.Show("Please, select output folder!");
                return;
            }


            for (var j = 0; j < openFileDialog1.FileNames.Length; j++)
            {
                var originalFileName = openFileDialog1.FileNames[j];
                var compactedFileName = openFileDialog2.FileNames[j];


                // reading original test file
                var fileStr = String.Empty;

                try
                {
                    // reading file
                    var streamReader = new StreamReader(originalFileName);
                    fileStr = streamReader.ReadToEnd();
                }
                catch (Exception)
                {
                    MessageBox.Show("Could not read original test file!");
                    return;
                }


                // reading original test
                var original = new BooleanSystem();
                var reader = new BooleanSystemReaderPlafd();
                reader.Input = fileStr;
                reader.System = original;

                try
                {
                    reader.Read();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(String.Format("Could not read original test! Details: {0}", ex.Message));
                    return;
                }



                // reading compact test file
                try
                {
                    // reading file
                    var streamReader = new StreamReader(compactedFileName);
                    fileStr = streamReader.ReadToEnd();
                }
                catch (Exception)
                {
                    MessageBox.Show("Could not read compacted test file!");
                    return;
                }


                // reading compact test
                var compacted = new BooleanSystem();
                reader.Input = fileStr;
                reader.System = compacted;

                try
                {
                    reader.Read();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(String.Format("Could not read compacted test from file {0}! Details: {1}", compactedFileName, ex.Message));
                    return;
                }


                var testSequence = new List<Cube>();

                try
                {
                    testSequence = PdfAnalyzer.BuildCompactedPdfTestSequence(original, compacted);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                

                // writing sequence to file

                var outputFileStr = String.Empty;

                outputFileStr += String.Format(".p {0}", testSequence.Count);
                outputFileStr += "\r\n";
                outputFileStr += "\r\n";

                Cube groupCube = null;
                foreach (var testVector in testSequence)
                {
                    if ((Cube)testVector.Data2 != groupCube)
                    {
                        groupCube = (Cube)testVector.Data2;
                        outputFileStr += "\r\n";
                    }

                    outputFileStr += String.Format("{0} Paths: {1}, Functions: {2}", testVector, testVector.Data, testVector.Data3);
                    outputFileStr += "\r\n";
                }

                outputFileStr += "\r\n";
                outputFileStr += "\r\n";
                outputFileStr += ".e";

                var outputFilePath = folderBrowserDialog1.SelectedPath + Path.DirectorySeparatorChar + Path.GetFileNameWithoutExtension(originalFileName) + "_seq.test";

                try
                {
                    var streamWriter = new StreamWriter(outputFilePath);
                    streamWriter.Write(outputFileStr);
                    streamWriter.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(String.Format("Output error: {0}.", ex.Message));
                    continue;
                }
            }


            MessageBox.Show("Complete!");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            openFileDialog2.ShowDialog();
        }
    }
}
